package com.sailmi.common.tool.crypto;

import com.sailmi.common.tool.StringPool;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.pqc.legacy.math.linearalgebra.ByteUtils;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Hex;

import java.security.KeyPair;

/**
 * signKey生成器
 *
 * @author Joffee
 */
public class Sm2KeyGenerator {

	public static void main(String[] args) {
		System.out.println("================ ucs.auth 配置如下 =================");
		KeyPair keyPair = null;
		try {
			keyPair = SM2Util.genKeyPair();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		String publicKey = SM2Util.getPubKeyHexString(keyPair.getPublic());
		String privateKey = SM2Util.getPriKeyHexString(keyPair.getPrivate());
		System.out.println("#加解密配置 \n" +
			"ucs:\n" +
			"  cryptor:\n" +
			"    public-key: " + publicKey + "\n" +
			"    private-key: " + privateKey);
		System.out.println("=======================================================");
		System.out.println(StringPool.EMPTY);
		System.out.println("============== saber website.js 配置如下 ===============");
		System.out.println("//saber配置\n" +
			"auth: {\n" +
			"  publicKey: '" + publicKey + "',\n" +
			"}");
		System.out.println("Open SSL:");
		System.out.println("Public Key:");
		try {
			System.out.println(BCECUtil.convertECPublicKeyX509ToPEM(keyPair.getPublic().getEncoded()));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		System.out.println("Private Key:");
		System.out.println("PKCS8:");
		try {
			System.out.println(BCECUtil.convertECPrivateKeyPKCS8ToPEM(keyPair.getPrivate().getEncoded()));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		try {
			System.out.println("PKCS1:");
			String pkcs1Key = BCECUtil.convertPrivateKeyFromPkcs8ToPkcs1(Base64.toBase64String(keyPair.getPrivate().getEncoded()));
			//System.out.println(pkcs1Key);
			System.out.println(BCECUtil.convertECPrivateKeyPKCS8ToPEM(Base64.decode(pkcs1Key)));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		System.out.println("=======================================================");
		System.out.println(StringPool.EMPTY);
		System.out.println("============== 密码:[admin] 加密流程如下 ================");
		String password = "admin";
		String encryptedData = null;
		String encryptedHexData = null;
		try {
			encryptedData = SM2Util.encryptBySM2(password, Base64.toBase64String(Hex.decode(publicKey)));
		} catch (InvalidCipherTextException e) {
			throw new RuntimeException(e);
		}
		String decryptedText = null;
		try {
			decryptedText = SM2Util.decryptBySM2(encryptedData, Base64.toBase64String(Hex.decode(privateKey)));
		} catch (InvalidCipherTextException e) {
			throw new RuntimeException(e);
		}
		System.out.println("加密前: " + password);
		System.out.println("加密后: " + encryptedData);
		System.out.println("加密后: " + ByteUtils.toHexString(Base64.decode(encryptedData)));
		System.out.println("加密后:" );
		System.out.println("解密后: " + decryptedText);
		System.out.println("请注意: 此密文为前端加密后调用token接口的密码参数");
		System.out.println("=======================================================");


	}

}
